package com.lhj.xxsc.order.service.impl;

import com.lhj.xxsc.order.entity.OrderEntity;
import com.lhj.xxsc.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxsc.common.utils.PageUtils;
import com.xxsc.common.utils.Query;

import com.lhj.xxsc.order.dao.OrderItemDao;
import com.lhj.xxsc.order.entity.OrderItemEntity;
import com.lhj.xxsc.order.service.OrderItemService;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<OrderItemEntity> page = this.page(
				new Query<OrderItemEntity>().getPage(params),
				new QueryWrapper<OrderItemEntity>()
		);

		return new PageUtils(page);
	}


	/**
	 * queues指定需要监听的队列
	 * Message 原生消息类型，头+体
	 * T 发送的消息类型
	 * Channel 当前通道
	 * 如果开启手动确认。那么消息在没被确认之前，就会一直是unacked，即使是 consumer宕机，会重新发送消息
	 */
	// @RabbitListener(queues = {"hello-java-queue"})

	/**
	 * @RabbitListener 类可以加
	 * @RabbitHandler 只能方法上 可以实现差异化
	 */
	@RabbitHandler
	public void recieveMessage(Message message, OrderEntity orderEntity, Channel channel) {
		// 消息内容
		byte[] body = message.getBody();
		// 消息头
		MessageProperties messageProperties = message.getMessageProperties();
		//  System.out.println("接收到的消息===OrderEntity》" + message + " 内容：" + orderEntity + "类型：" + message.getClass());
		System.out.println("接收到的消息===OrderEntity》 内容：" + orderEntity);
		// 通道内自增的
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		try {
			channel.basicAck(deliveryTag, false);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("网络中断");
		}

	}


	@RabbitHandler
	public void recieveMessage(Message message, OrderReturnReasonEntity orderReturnReasonEntity, Channel channel) {
		long deliveryTag = message.getMessageProperties().getDeliveryTag();
		try {
			channel.basicAck(deliveryTag, false);
//			channel.basicNack(deliveryTag, false, true);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("网络中断");
		}
		// 消息内容
		byte[] body = message.getBody();
		// 消息头
		MessageProperties messageProperties = message.getMessageProperties();
		//  System.out.println("接收到的消息===OrderReturnReasonEntity》" + message + " 内容：" + orderReturnReasonEntity + "类型：" + message.getClass());
		System.out.println("接收到的消息===OrderReturnReasonEntity》 内容：" + orderReturnReasonEntity);
	}

}
